// pages/user-shop/user-shop.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isShowCity: false, // 是否展开城市列表
    productInfo: [],
    imgUrl: 'https://tyc-bj.cn/yc/api/attach/',
    imgadinfo: [],
    hottype: [],// 热门分类
    searchPlaceholder: '请输入商品信息',
    operationgoodsnum: 6,
    yimeigoodsnum: 3
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.loadProduct();
    this.loadImgAd();
    this.shopHotTypes();
  },
  /**
     * 生命周期函数--监听页面隐藏
     */
  // 离开页面关闭城市列表
  onHide: function () {
    this.setData({
      isShowCity: false,
    })
  },

  // 加载术前术后,医美产品
  loadProduct: function() {
    var that = this;
    var cityid = wx.getStorageSync('cityid');
    wx.request({
      url: app.globalData.baseUrl + 'shopp/mallApi/indexShow?operationgoodsnum=' + that.data.operationgoodsnum + '&yimeigoodsnum=' + that.data.yimeigoodsnum + '&cityid=' + cityid, //仅为示例，并非真实的接口地址
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      success(res) {
        console.log('商城首页数据:', res.data.data)
        that.setData({
          productInfo: res.data.data,
        })
      }
    })
  },
  // 加载图片广告
  loadImgAd: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'system/advertise/getAdvertise',
      data: {
        'type': 0,
        'adType': 'shop',
      },
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
      },
      method: 'POST',
      success(res) {
        console.log('图片广告:', res.data.data)
        var tempData = res.data.data;
        for (var i = 0; i < tempData.length; i++) {
          if (tempData[i].srcType == 1) {
            tempData[i].srcUrl = '../user-shop-goods-details/user-shop-goods-details?etype=1&id=' + tempData[i].srcId;
          } else if (tempData[i].srcType == 2) {
            tempData[i].srcUrl = '../user-shop-goods-details/user-shop-goods-details?etype=2&id=' + tempData[i].srcId;
          }
        }
        that.setData({
          'imgadinfo': res.data.data,
        })
      }
    })
  },
  // 商城热门分类
  shopHotTypes: function (e) {
    var that = this;
    wx.request({
      url: app.globalData.baseUrl + 'shopp/goodsTypeApi/getHotTypes?type=2',
      data: {},
      header: {
        'sign': app.globalData.sign,
        'token': app.globalData.token,
        'content-type': 'application/json'
      },
      method: 'GET',
      success(res) {
        console.log("商城热门分类", res.data);
        that.setData({
          'hottype': res.data.data,
        })
      }
    })
  },
  // 商城首页搜索
  indexSearch: function (e) {
    console.log("商城首页搜索input:", e.detail.value)
    var keyword = e.detail.value;
  },
  showCity: function (e) {
  },
  // 点击市
  bindCity: function (e) {
    this.loadProduct();
  },
  tapIcon: function(e) {
    wx.navigateTo({
      url: '/pages/doctor-city-map/doctor-city-map'
    });
  }
})